package com.leased.controller;

import com.leased.enums.BusinessStatusEnum;
import com.leased.exception.YCException;
import com.leased.handler.ZUserHandler;
import com.leased.pojo.ZUsers;
import com.leased.request.zuser.ZUserListRequest;
import com.leased.request.zuser.ZUsersAddRequest;
import com.leased.response.PhoneNumberResponse;
import com.leased.response.UserCountResponse;
import com.leased.response.ZUserDetailResponse;
import com.leased.response.ZUserResponse;
import com.leased.utils.APIResponse;
import com.leased.utils.Page;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by shencx on 2017/7/28.
 */
@Controller
@RequestMapping(value = "/api/line_user")
public class ZUserController {

    final Logger LOG = LoggerFactory.getLogger(ZUserController.class);

    @Resource
    private ZUserHandler zUserHandler;

    /**
     * 获取专线用户列表
     * @param request
     * @param zUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<Page<ZUserResponse>> list(HttpServletRequest request, ZUserListRequest zUserListRequest){
        APIResponse<Page<ZUserResponse>> apiResponse = new APIResponse<>();
        Page<ZUserResponse> page = null;
        try {
            page = zUserHandler.getZUserInfoPage(zUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取专线用户列表发生异常", zUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 添加修改专线用户
     * @param request
     * @param zUsersAddRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editor",method = RequestMethod.POST)
    public APIResponse editor(HttpServletRequest request, ZUsersAddRequest zUsersAddRequest){
        APIResponse apiResponse = new APIResponse();
        try {
            zUserHandler.editZUserInfo(zUsersAddRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("添加修改专线用户信息发生异常", zUsersAddRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取专线详情
     * @param request
     * @param zUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public APIResponse<ZUsers> detail(HttpServletRequest request,ZUserListRequest zUserListRequest){
        APIResponse<ZUsers> apiResponse = new APIResponse<>();
        ZUsers zUsers = null;
        try {
            zUsers = zUserHandler.getZUserDetail(zUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(zUsers);
        } catch (Throwable e) {
            LOG.error("获取专线详情信息发生异常", zUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 根据主获取详情
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/particular",method = RequestMethod.GET)
    public APIResponse<ZUserDetailResponse> particular(HttpServletRequest request, Integer id){
        APIResponse<ZUserDetailResponse> apiResponse = new APIResponse<>();
        ZUserDetailResponse zUserDetailResponse = null;
        try {
            zUserDetailResponse = zUserHandler.getZUserDetailById(id);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(zUserDetailResponse);
        } catch (Throwable e) {
            LOG.error("根据主键获取专线详情信息发生异常", id);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    @ResponseBody
    @RequestMapping(value = "/selectByPhone",method = RequestMethod.GET)
    public APIResponse<PhoneNumberResponse> selectByPhone(HttpServletRequest request,String phoneNumber){
        APIResponse<PhoneNumberResponse> apiResponse = new APIResponse<>();
        PhoneNumberResponse phoneNumberResponse = null;
        try {
            phoneNumberResponse = zUserHandler.getUserByPhoneNumber(phoneNumber);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(phoneNumberResponse);
        } catch (Throwable e) {
            LOG.error("根据手机号码获取用户发生异常", phoneNumber);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取用户注册数
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userCount",method = RequestMethod.GET)
    public APIResponse<UserCountResponse> userCount(HttpServletRequest request){
        APIResponse<UserCountResponse> apiResponse = new APIResponse<>();
        UserCountResponse userCountResponse = null;
        try {
            userCountResponse = zUserHandler.getUserCount();
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(userCountResponse);
        } catch (Throwable e) {
            LOG.error("获取用户注册数发生异常");
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }
}
